package cn.stylefeng.guns.modular.businessArea.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.businessArea.entity.BusinessArea;
import cn.stylefeng.guns.modular.businessArea.enums.BusinessAreaExceptionEnum;
import cn.stylefeng.guns.modular.businessArea.mapper.BusinessAreaMapper;
import cn.stylefeng.guns.modular.businessArea.pojo.request.BusinessAreaRequest;
import cn.stylefeng.guns.modular.businessArea.service.BusinessAreaService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.file.api.constants.FileConstants;
import cn.stylefeng.roses.kernel.file.api.expander.FileConfigExpander;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 业务领域表业务实现层
 *
 * @author jizhongbao
 * @date 2022/01/10 15:07
 */
@Service
public class BusinessAreaServiceImpl extends ServiceImpl<BusinessAreaMapper, BusinessArea> implements BusinessAreaService {

    @Override
    public void add(BusinessAreaRequest businessAreaRequest) {
        BusinessArea businessArea = new BusinessArea();
        BeanUtil.copyProperties(businessAreaRequest, businessArea);
        this.save(businessArea);
    }

    @Override
    public void del(BusinessAreaRequest businessAreaRequest) {
        BusinessArea businessArea = this.queryBusinessArea(businessAreaRequest);
        this.removeById(businessArea.getId());
    }

    @Override
    public void edit(BusinessAreaRequest businessAreaRequest) {
        BusinessArea businessArea = this.queryBusinessArea(businessAreaRequest);
        BeanUtil.copyProperties(businessAreaRequest, businessArea);
        this.updateById(businessArea);
    }

    @Override
    public BusinessArea detail(BusinessAreaRequest businessAreaRequest) {
        return this.queryBusinessArea(businessAreaRequest);
    }

    @Override
    public PageResult<Map<String, Object>> findPage(BusinessAreaRequest businessAreaRequest) {
        LambdaQueryWrapper<BusinessArea> wrapper = createWrapper(businessAreaRequest);
        wrapper.eq(BusinessArea::getStatus, 1);
        wrapper.orderByAsc(BusinessArea::getSort);
        Page<Map<String, Object>> sysRolePage = this.pageMaps(PageFactory.defaultPage(), wrapper);
        for (Map<String, Object> businessArea : sysRolePage.getRecords()) {
            // 获取context-path
            String contextPath = HttpServletUtil.getRequest().getContextPath();
            String iconUrl = FileConfigExpander.getServerDeployHost() + contextPath + FileConstants.FILE_PUBLIC_PREVIEW_URL + "?fileId=" + businessArea.get("icon");
            businessArea.put("iconUrl", iconUrl);
        }
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Map<String, Object>> findList(BusinessAreaRequest businessAreaRequest) {
        LambdaQueryWrapper<BusinessArea> wrapper = this.createWrapper(businessAreaRequest);
        wrapper.eq(BusinessArea::getStatus, 1);
        wrapper.orderByAsc(BusinessArea::getSort);
        List<Map<String, Object>> businessAreas = this.listMaps(wrapper);
        for (Map<String, Object> businessArea : businessAreas) {
            // 获取context-path
            String contextPath = HttpServletUtil.getRequest().getContextPath();
            String logoUrl = FileConfigExpander.getServerDeployHost() + contextPath + FileConstants.FILE_PUBLIC_PREVIEW_URL + "?fileId=" + businessArea.get("icon");
            businessArea.put("iconUrl", logoUrl);
        }
        return businessAreas;
    }

    /**
     * 获取信息
     *
     * @author jizhongbao
     * @date 2022/01/10 15:07
     */
    private BusinessArea queryBusinessArea(BusinessAreaRequest businessAreaRequest) {
        BusinessArea businessArea = this.getById(businessAreaRequest.getId());
        if (ObjectUtil.isEmpty(businessArea)) {
            throw new ServiceException(BusinessAreaExceptionEnum.BUSINESSAREA_NOT_EXISTED);
        }
        return businessArea;
    }

    /**
     * 创建查询wrapper
     *
     * @author jizhongbao
     * @date 2022/01/10 15:07
     */
    private LambdaQueryWrapper<BusinessArea> createWrapper(BusinessAreaRequest businessAreaRequest) {
        LambdaQueryWrapper<BusinessArea> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = businessAreaRequest.getId();
        Long icon = businessAreaRequest.getIcon();
        String title = businessAreaRequest.getTitle();
        String introduction = businessAreaRequest.getIntroduction();
        Integer sort = businessAreaRequest.getSort();
        Integer status = businessAreaRequest.getStatus();

        queryWrapper.eq(ObjectUtil.isNotNull(id), BusinessArea::getId, id);
        queryWrapper.eq(ObjectUtil.isNotNull(icon), BusinessArea::getIcon, icon);
        queryWrapper.like(ObjectUtil.isNotEmpty(title), BusinessArea::getTitle, title);
        queryWrapper.like(ObjectUtil.isNotEmpty(introduction), BusinessArea::getIntroduction, introduction);
        queryWrapper.eq(ObjectUtil.isNotNull(sort), BusinessArea::getSort, sort);
        queryWrapper.eq(ObjectUtil.isNotNull(status), BusinessArea::getStatus, status);

        return queryWrapper;
    }

}